package Stack;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 石方旭
 * Date: 2022-05-10
 * Time: 16:41
 */
//push pop peek  Empty  size
public class MyStack {
    private int[] elem;
    private int usedSize;

    public MyStack(){
        this.elem = new int[4];
        this.usedSize=0;
    }

    public void push(int val){
        //检查容量满了需要增容
       if(this.usedSize==this.elem.length){
           this.elem = Arrays.copyOf(this.elem,this.elem.length*2);
       }
        this.elem[usedSize++]=val;
    }

    public void pop(){
        if(this.usedSize==0){
            System.out.println("栈中没有元素无法删除！！！");
        }
        usedSize--;
    }

    public int peek(){
        if(this.usedSize==0){
            System.out.println("无法显示栈顶元素---》this.usedSize==0");
        }
        return elem[this.usedSize];
    }

    public Boolean Empty(){
        return this.usedSize==0;
    }

    public int size(){
        return this.usedSize;
    }

    public void display(){
        for(int i =0;i<this.usedSize;++i){
            System.out.print(this.elem[i]+" ");
        }
        System.out.println();
    }

}
